package com.example.webdome.controller;



import com.example.webdome.pojo.SpiderDto;
import com.example.webdome.service.impl.SpiderService;

import com.example.webdome.util.urlCount;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@RestController

@RequestMapping("/spider")
public class Spider {

    @Resource
    SpiderService spiderService;
    @Resource
    private urlCount Count;


    //@ApiOperation(value = "获取图标")
    @GetMapping("/getIcon")
    public SpiderDto searchAppByName(@RequestParam String name, HttpServletRequest request) {
        String uri = request.getRequestURI();
        Count.recordUrlCount(uri);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.set(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36");

        HttpEntity<String> entity = new HttpEntity<>(headers);

        String url = "https://itunes.apple.com/search?term=" + name + "&country=cn&entity=software&limit=12&callback=jsonp_1695208077236_88151";
        String url2 = "https://itunes.apple.com/search?term=" + name + "&country=us&entity=software&limit=12&callback=jsonp_1695208077236_88151";

        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        ResponseEntity<String> responseEntity2 = restTemplate.exchange(url2, HttpMethod.GET, entity, String.class);
        String responseBody = responseEntity.getBody();
        String responseBody2 = responseEntity2.getBody();

        return spiderService.getUrl(spiderService.getJsonPart(responseBody),spiderService.getJsonPart(responseBody2),name);

        //spiderService.getJsonPart(responseBody);

    }



}
